package Day9Zuoye;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class Xmlchaoshi {

    SAXReader reader = new SAXReader();
    Document doc = null;
    public  static String path = "D:\\76班java workspace\\Y2Java项目\\JavaSenior\\src\\Day9Zuoye\\products.xml";


    public void show() {
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("根节点：" + doc.getRootElement().getName());
        List<Element> list = doc.selectNodes("/shop/product");
        for (Element e : list) {
            String discount = e.attributeValue("discount");
            String factoryaddr = e.attributeValue("factoryaddr");
            int itemno = Integer.parseInt(e.selectSingleNode("itemno").getText());
            Element name = (Element) e.selectSingleNode("name");
            String sname = name.getText();
            int price = Integer.parseInt(e.selectSingleNode("price").getText());
            int stockcount = Integer.parseInt(e.selectSingleNode("stockcount").getText());
            String unit = e.selectSingleNode("unit").getText();
            System.out.println("是否折扣:"+discount + ","+"工厂地址:"+ factoryaddr + ","+"商品编号:"+itemno+"," +"商品名称:"+ sname + "," +"商品单价:"+price + "," +"库存量:"+stockcount + ","+"计价单位:"+unit);
        }
    }

    public boolean selectItemno(int  itemno){
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        }

        List<Element> list = doc.selectNodes("/shop/product[@itemno='"+itemno+"']");
        if (list.size()==0){
            System.out.println("没有，滚");
            return false;
        }
        for (Element e:list){
            int itemno1 = Integer.parseInt(e.selectSingleNode("itemno").getText());
            Element name = (Element) e.selectSingleNode("name");
            String sname = name.getText();
            int price = Integer.parseInt(e.selectSingleNode("price").getText());
            int stockcount = Integer.parseInt(e.selectSingleNode("stockcount").getText());
            String unit = e.selectSingleNode("unit").getText();
            System.out.printf("商品编号:%s 商品名称:%s 商品单价:%s 库存量:%s 计价单位:%s \n",itemno1,sname,price,stockcount,unit);
            return true;
        }
        return false;
    }

    public boolean add(boolean discount,String factoryaddr,int itemno,String sname,double price,int stockcount,String unit){
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        }
        Element root = doc.getRootElement();
        Element newshop = root.addElement("shop");
        newshop.addAttribute("discount",String.valueOf(discount));
        newshop.addAttribute("factoryaddr",factoryaddr);
        List<Element> list = doc.selectNodes("/shop/product[@itemno='"+itemno+"'or sname='"+sname+"']");
        if (list.size()==0){
            newshop.addElement("itemno").setText(String.valueOf(itemno));
            newshop.addElement("sname").setText(sname);
        }else{
            System.out.println("序列号or名字重复,退出");
            return false;
        }
        newshop.addElement("price").setText(String.valueOf(price));
        newshop.addElement("stockcount").setText(String.valueOf(stockcount));
        newshop.addElement("unit").setText(unit);
        //节点更新完成
        //写回xml
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        try {
            XMLWriter xm = new XMLWriter(new FileWriter(path), format);
            xm.write(doc);
            xm.close();
            System.out.println("xie ru chenggong");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean Modifyskct(int itemno,int stockcount){
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        }
        //根据序列号查找，更改
        Element e = (Element) doc.selectSingleNode("/shop/product[@itemno='"+itemno+"']");
        if(e==null)
        {
            System.out.println("not in.");
            return false;
        }
        e.selectSingleNode("stockcount").setText(String.valueOf(stockcount));
        return true;
    }
    
    public boolean goumai(int itemno, int shuliang) {
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        }
        List<Element> list = doc.selectNodes("/shop/product[@itemno='" + itemno + "']");
        if (list.size() == 0) {
            System.out.println("无此商品");
            return false;
        }
        for (Element e : list) {
            Element estockcount = (Element) e.selectSingleNode("stockcount");
            int stockcount = Integer.parseInt(estockcount.getText());

            Element eprice = (Element) e.selectSingleNode("price");
            Double price = Double.parseDouble(eprice.getText());
            boolean product = Boolean.valueOf(e.attributeValue("discount"));

            if (shuliang > stockcount || shuliang < 0) {
                System.out.println("库存不足或输入错误");
                return false;
            } else {
                e.selectSingleNode("stockcount").setText(String.valueOf(stockcount - shuliang));
                try {
                    OutputFormat format = OutputFormat.createPrettyPrint();
                    format.setEncoding("utf-8");
                    XMLWriter xw = new XMLWriter(new FileOutputStream(path), format);
                    xw.write(doc);
                    xw.close();
                } catch (Exception o) {
                    o.printStackTrace();
                }
                if (product) {
                    double dz = price * shuliang * 0.7;
                    System.out.println("应付款：" + dz + "元");
                } else {
                    double zj= price * shuliang;
                    System.out.println("应付款：" + zj + "元");
                }
                return true;
            }
        }
       return false;
    }
}


